define(function (require, exports, module) {
    require("apps/lab/page.css");
    require("apps/grid");
    require("apps/easyui");

    exports.createDialog = function (ctx, config) {
        var uuid = $.utils.createUUID(12);

        var template = '<div><div class="box_tc_s"><div class="grid-wrap" style="margin-top: 0">' +
            '<table data-id="grid2"></table></div></div><div data-id="user_list_btn2">' +
            '<a data-href="#" data-id="{{id}}" data-key="view" iconCls="fa fa-eye fa-lg" plain="true">详情查看</a>' +
            '</div></div>';

        var panel = $(template).appendTo("body").attr("id", uuid);

        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid2",
                colbar: "user_list_btn2"
            },
            configs: {
                gridUrl: ctx + "/app/api/ebom/list?status=PUBLISH",
                idField: "id",
                queryParams: $.utils.buildJson(formStr),
                treeField: "eName",
                gridColumns: [
                    {
                        field: 'id',
                        hidden: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'eName',
                        title: '名称',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'pBomDes',
                        title: '设计结构',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: "cover", title: "图片",
                        hidden: true, formatter: function (value, row, index) {
                        return "<img src=\"" + ctx + "/print/" + value + "\" style=\"width:48px;height:32px;\"/>"
                    }
                    },
                    {
                        field: 'bomType',
                        title: '是否装配',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "E_BOM_TYPE_BUILD") {
                                return "已装配";
                            } else {
                                return "未装配";
                            }
                        }
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        hidden: true,
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'description',
                        title: '功能',
                        width: 50,
                        align: 'left',
                        hidden: true
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        width: 100,
                        align: 'left'
                    }
                ]
            },
            events: {
                renderColBarBefore: function (key, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var formStr = config.formStr;
        formStr = formStr.length ? JSON.parse(formStr) : {};

        var _globalWindow = new $.PageWindow();

        function whenView(context, row) {
            _globalWindow.create({
                href: ctx + "/app/api/ebom/view?id=" + row.id,
                width:800,
                title: row.eName + "的结构查看"
            });
            _globalWindow.open();
        }

        panel.dialog({
            width: 600,
            height: 500,
            closable: true,
            loadingMessage: '页面加载中，请稍后...',
            title: "匹配条件的所有工件",
            onClose: function () {
                panel.dialog("destroy");
                panel.remove()
            },
            buttons: [
                {
                    text: "关闭",
                    handler: function () {
                        panel.dialog("destroy");
                        panel.remove()
                    }
                }
            ]
        });

        return panel;
    }
});